<template>
	<a-card :bordered="false">
		<div class="table-page-search-wrapper">
			<a-form layout="inline">
				<a-row :gutter="48">

					<a-col :md="8" :sm="24">
						<a-form-item label="生产单号">
							<a-input v-model="queryParam.orderNumber" placeholder="请输入生产单号" />
						</a-form-item>
					</a-col>
					<a-col :md="24" :sm="24" v-if="canReadOrgIdCompany()">
						<a-form-item label="归属公司：">
							<FCOrgRadioGroup v-model="queryParam.orgId" :filter-types="[$businessEnum.ORG_TYPE_ENUM.COMPANY.id]" />
						</a-form-item>
					</a-col>
					<a-col :md="6" :sm="6">
						<a-form-item label="物料编号">
							<a-input v-model="queryParam.jzMaterialCode" placeholder="请输入物料编号" />
						</a-form-item>
					</a-col>
					<a-col :md="6" :sm="6">
						<a-form-item label="领用方">
							<a-select placeholder="全部" v-model="queryParam.classGroupId">
								<a-select-option :values="item.classGroupId" v-for="item in teamList" :key="item.classGroupId">
									{{ item.name }}
								</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>

					<a-col :md="4" :sm="24">
						<span class="table-page-search-submitButtons" :style="{ float: 'right', overflow: 'hidden' } || {}">
							<a-button type="primary" @click="table.refresh(true)">查询</a-button>
							<a-button style="margin-left: 8px"
								@click="() => { queryParam.orderNumber = null; queryParam.orgId = null; queryParam.jzMaterialCode = null; queryParam.classGroupId = undefined; table.refresh(true) }">重置</a-button>
						</span>
					</a-col>
				</a-row>
			</a-form>
		</div>

		<s-table ref="table" size="default" :columns="columns" :data="loadData" :alert="true"
			:rowKey="(record) => record.code">

			<span slot="action" slot-scope="text, record">
				<a @click="productionOrderMaterialsOutStoreDetailForm.show(record, '出库详情', '', { materialType })">出库详情</a>
				<a-divider type="vertical" />
				<a
					@click="outStoreOrderModal.show(record, '出库明细', 'outOrder', { 'orderType': $businessEnum.ORDER_TYPE_ENUM.outOrder.id })">出库明细</a>
				<a-divider type="vertical" />
				<a @click="backStoreDetailModal.show(record)">退库详情</a>
				<a-divider type="vertical" />
				<a @click="backStoreRecordDetailModal.show(record)">退库入库单明细</a>
			</span>

		</s-table>

		<production-order-materials-out-store-detail-form ref="productionOrderMaterialsOutStoreDetailForm" />
		<out-store-order-modal ref="outStoreOrderModal" />
		<back-store-record-detail-modal ref="backStoreRecordDetailModal" />
		<back-store-detail-modal ref="backStoreDetailModal" />
	</a-card>
</template>

<script lang="ts">
import { Component, Vue, Ref, Watch } from "vue-property-decorator";

import { STable } from '@/components'
import { page } from '@/api/modular/main/emsorder/emsOrderManage'
import FCOrgRadioGroup from '@/components/FcOrgRadioGroup/FCOrgRadioGroup.vue'

import productionOrderMaterialsOutStoreDetailForm
	from '@/views/main/emsordermaterial/productionOrder/emsOutStoreDetailForm.vue'
import outStoreOrderModal from '@/views/main/emsorder/productionOrder/emsProductionOrderOutStoreOrderModal.vue'
import BackStoreRecordDetailModal from './backStoreRecordDetailModal.vue'
import BackStoreDetailModal from './backStoreDetailModal.vue'

import { page as teamPage } from '@/api/modular/main/classGroup/classGroup'

@Component({
	components: {
		STable,
		productionOrderMaterialsOutStoreDetailForm,
		outStoreOrderModal,
		FCOrgRadioGroup,
		BackStoreRecordDetailModal,
		BackStoreDetailModal
	}
})

export default class ProductionOrderOutStoreCompleted extends Vue {
	@Ref() readonly table!: any
	@Ref() readonly productionOrderMaterialsOutStoreDetailForm!: any
	@Ref() readonly outStoreOrderModal!: any
	@Ref() readonly backStoreRecordDetailModal!: BackStoreRecordDetailModal
	@Ref() readonly backStoreDetailModal!: BackStoreDetailModal

	created() {
		this.getTeamList();
	}
	queryParam = {
		orderNumber: '',
		orderType: '',
		executeStatus: '',
		materialType: '',
		jzMaterialCode: '',
		classGroupId:undefined,
	}
	teamList = []
	columns = [
		{
			title: '序号', width: '80px',
			customRender: (text, record, index) => `${index + 1}`
		},
		{
			title: '生产单号',
			dataIndex: 'orderNumber'
		},
		{
			title: '开始时间',
			dataIndex: 'createTime'
		},
		{
			title: '结束时间',
			dataIndex: 'completedTime'
		},
		{
			title: '操作',
			dataIndex: '',
			width: '360px',
			scopedSlots: { customRender: 'action' }
		}
	]


	@Watch('$route.path')
	watchRoutePath(newValue: string) {
		setTimeout(() => {
			this.table.clearSelected()
			this.table.refresh(true)
		}, 100)
	}


	get routePath() {
		let urlParam = this.$util.getUrlParam(this.$route.path)
		return urlParam
	}

	get materialType() {
		return this.routePath?.materialType || this.$businessEnum.MATERIAL_TYPE_ENUM.material.id
	}

	//获取领用方
	getTeamList() {
		teamPage({
			pageSize: -1,
			status: 0
		}).then(res => {
			this.teamList = res.data.rows;
		})
	}
	// 加载数据方法 必须为 Promise 对象
	loadData(parameter) {
		/*
		生产单已出库查询的是已经结束的生产单(指进行中的后一步).
		 */
		this.queryParam.orderType = this.$businessEnum.ORDER_TYPE_ENUM.produceOrder.id
		this.queryParam.executeStatus = this.$businessEnum.EXECUTE_STATUS_ENUM.COMPLETED.id
		this.queryParam.materialType = this.materialType
		return page(Object.assign(parameter, this.queryParam)).then((res) => {
			return res.data
		})
	}


	handleOk() {
		this.table.refresh()
		this.table.clearSelected()
	}

	canReadOrgIdCompany() {
		return !this.hasRole(this.$util.ROLE_ENUM.GS_ADMIN.id) && !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN3.id)
	}

}
</script>

<style lang="less">
.table-operator {
	margin-bottom: 18px;
}

button {
	margin-right: 8px;
}
</style>
